package com.elinkway.infinitemovies.http.parser;

import android.text.TextUtils;

import com.elinkway.infinitemovies.bean.LiveClassifyInfos;
import com.elinkway.infinitemovies.bean.LivePlaytimeInfo;
import com.elinkway.infinitemovies.bean.LiveTvStationDetail;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class LiveChannelParser extends LVideoMobileParser<LiveClassifyInfos> {
	@Override
	public LiveClassifyInfos parse(JSONObject data) throws Exception {
//		LiveInfos liveInfos = null;
		LiveClassifyInfos classifyInfo = new LiveClassifyInfos();
		if (data.has("data")) {
			JSONArray dataArray = data.optJSONArray("data");
//			int infoSize = dataArray.length();
//			ArrayList<LiveClassifyInfos> infoLists = new ArrayList<LiveClassifyInfos>(
//					infoSize);
			// 先去直播分类信息
			JSONObject classifyObj = dataArray.optJSONObject(0);

			JSONArray channelArray = classifyObj.optJSONArray("channels");
			int channelSize = channelArray.length();
			ArrayList<LiveTvStationDetail> tvStationList = new ArrayList<LiveTvStationDetail>(
					channelSize);
            for (int j = 0; j < channelSize; j++) {
                LiveTvStationDetail liveStationDetail = new LiveTvStationDetail();
                JSONObject channelObj = channelArray.optJSONObject(j);
                
                JSONArray streams = channelObj.optJSONArray("streams");
                String url = channelObj.optString("url");
                
                if ((null != streams && streams.length() > 0) || !TextUtils.isEmpty(url)) {
                    int streamsSize = streams.length();
                    ArrayList<String> streamsList = new ArrayList<String>(streamsSize);
                    
                    if (!TextUtils.isEmpty(url)){
                        streamsList.add(url);
                    }
                    
                    for (int i = 0; i < streamsSize; i++) {
                        String streamUrl = streams.optString(i);
                        if (!TextUtils.isEmpty(streamUrl)) {
                            streamsList.add(streamUrl);
                        }
                    }
                  
                    liveStationDetail.setStreams(streamsList);

                    liveStationDetail.setId(channelObj.optString("id"));
                    liveStationDetail.setIcon(channelObj.optString("icon"));
                    liveStationDetail.setName(channelObj.optString("name"));

                    JSONArray currentPlayArry = channelObj.optJSONArray("current");
                    int currentPlaySize = 0;
                    if (currentPlayArry != null) {
                        currentPlaySize = currentPlayArry.length();
                        ArrayList<LivePlaytimeInfo> playTimeList =
                                new ArrayList<LivePlaytimeInfo>(currentPlaySize);
                        for (int m = 0; m < currentPlaySize; m++) {
                            LivePlaytimeInfo playTimeInfo = new LivePlaytimeInfo();
                            JSONObject playTimeObj = currentPlayArry.optJSONObject(m);
                            playTimeInfo.setPlayTime(playTimeObj.optString("playtime"));
                            playTimeInfo.setTitle(playTimeObj.optString("title"));
                            // 添加每次解析的播放时间信息
                            playTimeList.add(playTimeInfo);
                        }
                        liveStationDetail.setPlayTimeList(playTimeList);
                    }
                    // 添加每个电视台的播放数据
                    tvStationList.add(liveStationDetail);
                }
            }
            
            
            classifyInfo.setmChannels(tvStationList);
		}
		return classifyInfo;
	}
}
